module.exports = (app) => {
    /**
     * 项目控制器
     * 继承自基础控制器
     */
    const BaseController = require('./base')(app)
    return class ProjectController extends BaseController {
        async getModelList(ctx) {
            const {project: projectService} = app.services
            const modelList = await projectService.getModelList() 

            // 只返回关键数据
            const dtoModelList = modelList.reduce((preList, item) => {
                const { model, project } = item
                const { key, name, desc } = model
                const dtoModel = {
                    key,
                    name,
                    desc,
                }

                const dtoProject = Object.keys(project).reduce((proj, projKey) => {
                    const {key, name, desc, homePage} = project[projKey]
                    proj[projKey] = {
                        key,
                        name,
                        desc,
                        homePage,
                    }
                    return proj
                }, {})

                preList.push({
                    model: dtoModel,
                    project: dtoProject,
                })
                return preList
            }, [])
            this.success(ctx, dtoModelList)
        }
        async getList(ctx) {
            const {proj_key: projKey} = ctx.request.query
            const {project: projService} = app.services
            const projectList = projService.getList({projKey})
            const dtoProjectList = []
            for (let i = 0; i < projectList.length; ++i) {
                const { modelKey = '', key, name, desc, homePage } = projectList[i];
                dtoProjectList.push({ modelKey, key, name, desc, homePage });
            }
            this.success(ctx, dtoProjectList);
        }
        async get(ctx) {
            const {proj_key: projKey} = ctx.request.query
            const {project: projService} = app.services
            const projConfig = projService.get(projKey)
            if (!projConfig) {
                this.fail(ctx, '获取项目异常', 500)
                return
            }
            this.success(ctx, projConfig)
        }
    }
}